package cn.ven.controller;


import cn.ven.tool.RespJson;
import org.slf4j.Logger;

import org.slf4j.LoggerFactory;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@ControllerAdvice  //环绕
@RestController
//捕捉异常的类
public class ErrController {
    private static Logger logger= LoggerFactory.getLogger(ErrController.class);

    //捕捉全部异常
    @ExceptionHandler(value = Exception.class)
    public Map<String,Object> errorHandler(Exception e){
        logger.error("e.message==="+e.getMessage());
        logger.error("e.getCause==="+e.getCause().toString());
        return RespJson.pack(100,e.getCause().toString(),"");
    }

    //捕捉token比对错误的异常
    @ExceptionHandler(value = OAuth2Exception.class)
    public Map<String, Object> handleOauth2(OAuth2Exception e) {
        logger.error("e.message==="+e.getMessage());
        logger.error("token error cause"+e.getCause().toString());
        return RespJson.pack(401,  "Token 验证错误", "");
    }


}
